package com.itheima.easy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.easy.base.AjaxResult;
import com.itheima.easy.base.PageResponse;
import com.itheima.easy.constant.PendingTasksConstant;
import com.itheima.easy.dto.PendingTasksDto;
import com.itheima.easy.entity.CheckIn;
import com.itheima.easy.entity.PendingTasks;
import com.itheima.easy.mapper.PendingTasksMapper;
import com.itheima.easy.mapper.UserRoleMapper;
import com.itheima.easy.service.PendingTasksService;
import com.itheima.easy.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sjqn
 * @date 2023/6/21
 */
@Service
public class PendingTasksServiceImpl implements PendingTasksService {

    @Autowired
    private PendingTasksMapper pendingTasksMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 条件分页查询待办任务
     * @param pendingTasksDto
     * @return
     */
    @Override
    public AjaxResult selectByPage(PendingTasksDto pendingTasksDto) {
        PageHelper.startPage(pendingTasksDto.getPageNum(),pendingTasksDto.getPageSize());
        Page<List<PendingTasks>> page = pendingTasksMapper.selectByPage(pendingTasksDto);
        PageResponse<PendingTasks> of = PageResponse.of(page, PendingTasks.class);
        return AjaxResult.success(of);
    }


    @Override
    public void insert(Long userId, CheckIn checkIn, Integer status) {
        //查询下一个审核人的姓名和角色
        List<UserRoleVo> userRoleVoList = userRoleMapper.selectByUserId(userId);
        //待办任务，下个节点审核人 设置下个操作人
        UserRoleVo userRoleVo = userRoleVoList.get(0);
        PendingTasks pendingTasks = pendingTasksMapper.selectByCodeAndAssinessId(checkIn.getCheckInCode(), userRoleVo.getId());
        if (pendingTasks != null) {
            pendingTasksMapper.updateIsHandle(checkIn.getCheckInCode(), PendingTasksConstant.TASK_IS_HANDLE_UNTREATED, userRoleVo.getId());
        } else {
            pendingTasks = getPendingTasks(checkIn, status);
            pendingTasks.setAssignee(userRoleVo.getUserName());
            pendingTasks.setAssigneeId(userRoleVo.getId());
            pendingTasksMapper.insert(pendingTasks);
        }
    }

    @Override
    public PendingTasks getPendingTasks(CheckIn checkIn, Integer accraditationStepNoOne) {
        PendingTasks pendingTasks = new PendingTasks();
        pendingTasks.setCode(checkIn.getCheckInCode());
        pendingTasks.setApplicat(checkIn.getApplicat());
        pendingTasks.setApplicatId(checkIn.getApplicatId());
        pendingTasks.setTitle(checkIn.getTitle());
        //退住
        pendingTasks.setType(PendingTasksConstant.TASK_TYPE_RETREAT);
        //审核中
        pendingTasks.setStatus(PendingTasksConstant.TASK_STATUS_APPLICATION);
        //未处理
        pendingTasks.setIsHandle(PendingTasksConstant.TASK_IS_HANDLE_UNTREATED);
        pendingTasks.setApplicationTime(checkIn.getCreateTime());
        pendingTasks.setStepNo(accraditationStepNoOne);
        return pendingTasks;
    }
}
